#include <bits/stdc++.h>
#define ll long long
#define i128 __int128
using namespace std;
ll T,n,a[200005],dp[1000005];
void solve()
{
	scanf("%lld",&n);
	for(ll i = 1;i <= n;i++)
		scanf("%lld",&a[i]);
	memset(dp,-0x3f,sizeof dp);
	dp[0] = 0;
	ll tag = 0,mx = 0;
	for(ll i = 2;i <= n;i++)
	{
		ll tmp = max(dp[mx] + tag,dp[a[i]] + tag + a[i]);
		tag += (a[i] == a[i - 1] ? a[i] : 0);
		dp[a[i - 1]] = max(dp[a[i]],tmp - tag);
		if(dp[a[i - 1]] > dp[mx])
			mx = a[i - 1];/*
		for(ll j = 0;j <= 20;j++)
			if(dp[j] >= 0)
				printf("%lld ",dp[j]);
			else
				printf("- ");
		printf("\n");*/
	}
	ll ans = 0;
	for(ll i = 0;i <= 1000000;i++)
		ans = max(ans,dp[i] + tag);
	printf("%lld\n",ans);
}
int main()
{
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	scanf("%lld",&T);
	while(T--)
		solve();
	return 0;
}